package com.chaoxing.ai.speech.Controller;

import com.chaoxing.ai.speech.service.SpeechService;
import com.chaoxing.ai.speech.utils.SamplingRateTest;
import com.chaoxing.ai.speech.utils.SoundConvert;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.MultimediaInfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("/a/speech")
public class SpeechController {
    @Resource
    private SoundConvert soundConvert;
    @Resource
    private SpeechService speechService;

    @RequestMapping(value = "/recognition",method = RequestMethod.POST)
    public String recognition(@RequestPart(value = "file") MultipartFile file) throws IOException {

        System.out.println("接收到的文件后缀为："+file.getOriginalFilename());
        String fileName = file.getOriginalFilename();

        String templatePath = "F:\\test";//上传文件暂存地址
        // 获取文件的后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf(".")+1);
        //获取文件名
        String prefixName = fileName.substring(0, fileName.lastIndexOf("."));
        //获取文件类型
        String contentType = file.getContentType();

        long size = file.getSize();
        //long size = file.length();
        System.out.println(suffixName);
        if(!suffixName.equals("wav")&&!suffixName.equals("mp3") && !suffixName.equals("m4a") && !suffixName.equals("pcm") && !suffixName.equals("amr")){
            return "文件格式错误!";
        }else if(size>1024*1024){
            return "文件大小错误";
        }


        File dest0 = new File(templatePath);
        File dest = new File(dest0, File.separator + fileName);
        //文件上传-覆盖
        try {

            if (!dest0.getParentFile().exists()){
                dest0.getParentFile().mkdirs();
            }
            if (!dest.exists()){
                dest.mkdirs();
            }
            file.transferTo(dest);

            String newfilePath = null;
            //arm音频转为MP3格式
            if(suffixName.equals("amr")){
                suffixName="mp3";
                String amrfilePath=templatePath+"\\"+fileName;
                fileName=prefixName+"."+suffixName;
                newfilePath=templatePath+"\\"+fileName;
                SoundConvert.amrToMp3(amrfilePath,newfilePath);
                dest.delete();                                             //删除amr音频文件
                dest=new File(dest0, File.separator + fileName);      //指向amr转MP3音频文件
            }

            //mp3格式音频转为pcm格式音频
            if(suffixName.equals("mp3")) {
                suffixName="pcm";
                String mp3filePath=templatePath+"\\"+fileName;
                fileName=prefixName+"."+suffixName;
                newfilePath=templatePath+"\\"+fileName;
                SoundConvert.Mp3ToPcm(mp3filePath,newfilePath);
                dest.delete();                                              //删除MP3音频文件
                dest=new File(dest0, File.separator + fileName);      //指向MP3转PCM音频文件
            }
            //pcm格式音频转换为wav格式
            Integer integer=null;
            if(suffixName.equals("pcm")) {
                suffixName="wav";
                String filePath = templatePath + "\\" + fileName;         //需要进行转化的文件
                fileName=prefixName+"."+suffixName;
                newfilePath=templatePath+"\\"+fileName;                   //存储的新的文件名
                integer = SamplingRateTest.frequency(dest.getPath());
                if(integer >= 14000 && integer <= 2502556) {
                    soundConvert.pcmToWav16K(filePath, newfilePath);
                }else{
                    soundConvert.pcmToWav8K(filePath, newfilePath);
                }
                dest.delete();                                         //删除PCM音频文件
                dest=new File(dest0, File.separator + fileName);  //指向PCM转换WAV的音频文件
            }
            integer=SamplingRateTest.frequency(dest.getPath());         //获取采样频率
            String  language = null;
            String filePath=templatePath+"\\"+fileName;
            File source = new File(filePath);
            Encoder encoder = new Encoder();
            MultimediaInfo m = encoder.getInfo(source);
            Double isTime30s=Double.parseDouble(m.getDuration()/1000+"");
            if(isTime30s>=30.0){
                return "Time Error!";
            }
            if(integer>=14000){
                language =speechService.speechRecognition(templatePath,fileName,suffixName);
            }else {
                language =speechService.speechRecognition8(templatePath,fileName,suffixName);
            }
            dest.delete();                          //删除WAV音频文件
            return language;
        }catch (Exception e){
            e.printStackTrace();
            return "资源发生错误";
        }
    }

}
